﻿namespace LogManager.Utils.Unity
{
    using Microsoft.Practices.Unity;
    using System;

    /// <summary>
    /// A simple Lazy singleton for the unity container. The UnityContainer
    /// is configures for Interception within the SetUp() method 
    /// </summary>
    public class DI : IDisposable
    {
        #region Data
        private static readonly Lazy<DI> instance
            = new Lazy<DI>(() => new DI());

        private IUnityContainer container;
        #endregion

        #region Ctor
        private DI()
        {
            container = new UnityContainer();
        }
        #endregion

        #region Public Methods/Properties

        public IUnityContainer UnityContainer
        {
            get { return container; }
        }

        public void SetUp(Action<IUnityContainer> ConfigureMethod)
        {
            ConfigureMethod(container);
        }

        public void Dispose()
        {
            if (container != null)
            {
                container.Dispose();
            }

            container = null;
        }
        #endregion

        public static DI Instance
        {
            get
            {
                return instance.Value;
            }
        }

        public static IUnityContainer Container
        {
            get
            {
                return Instance.UnityContainer;
            }
        }
    }
}
